﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Data.SqlClient;
using System.Configuration;

namespace ECSS.Winform
{
    public partial class FrmCancelBossOrders : Form
    {
        private BOSSInterfaceInvoke bossInterface;        

        public FrmCancelBossOrders()
        {
            InitializeComponent();
            bossInterface = new BOSSInterfaceInvoke();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            string log = txtResults.Text.Trim();

            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(txtOrderIds.Text.Trim())
                && txtOrderIds.Lines.Count() > 0)
            {
                for (int i = 0; i < txtOrderIds.Lines.Count(); i++)
                {
                    string orderid = txtOrderIds.Lines[i];
                    if (string.IsNullOrEmpty(orderid)) continue;
                    try
                    {
                        ResBOSSInvokeEntityOfstring s = bossInterface.BOSSOrderCancel(GetBossOrderIdByEcssId(orderid));

                        sb.Append("取消[" + GetBossOrderIdByEcssId(orderid) + "]:" + s.Succeedk__BackingField + "," + s.Messagek__BackingField + "," + s.ErrorMessagek__BackingField + "\r\n");
                    }
                    catch (Exception ex)
                    {
                        sb.Append(ex.Message + "\r\n");
                    }

                    txtResults.Text = sb.ToString();

                    Thread.Sleep(500);
                }
            }
        }

        private string GetBossOrderIdByEcssId(string ecssOrderId)
        {
            string sql = "select bossorderid from orderinfo where orderid=@orderid";

            SqlConnection sqlconn = new SqlConnection();
            sqlconn.ConnectionString = ConfigurationManager.ConnectionStrings["ECSSDb"].ConnectionString;

            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.Connection = sqlconn;
            sqlcmd.CommandText = sql;

            sqlcmd.Parameters.Add("@orderid",SqlDbType.NVarChar,20);
            sqlcmd.Parameters["@orderid"].Value = ecssOrderId;

            try
            {
                sqlcmd.Connection.Open();
                return sqlcmd.ExecuteScalar().ToString();
            }
            finally
            {
                sqlcmd.Connection.Close();
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtOrderIds.Text = string.Empty;
        }
    }
}
